package com.api.workflow.web;

import com.api.workflow.util.YNHttpUtils;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import weaver.common.util.string.StringUtil;
import weaver.conn.RecordSet;
import weaver.general.BaseBean;
import weaver.general.Util;
import weaver.interfaces.workflow.action.Action;
import weaver.soa.workflow.request.MainTableInfo;
import weaver.soa.workflow.request.Property;
import weaver.soa.workflow.request.RequestInfo;

import java.util.HashMap;
import java.util.Map;

/*
 * MMI030物料主数据创建接口
 *
 * */
public class YNMMI030Action extends BaseBean implements Action {


    @Override
    public String execute(RequestInfo requestInfo) {
        BaseBean baseBean = new BaseBean();
        String requestid = requestInfo.getRequestid();
        int formid = requestInfo.getRequestManager().getFormid();
        int formidAbs = Math.abs(formid);// 流程表单id绝对值
        String tableName = "formtable_main_" + formidAbs;

        // 主表数据
        MainTableInfo mainTableInfo = requestInfo.getMainTableInfo();
        Property[] property = mainTableInfo.getProperty();
        Map<String, Object> mainTableInfoMap = new HashMap<>();
        for (int i = 0; i < property.length; i++) {
            String name = property[i].getName().toLowerCase();
            mainTableInfoMap.put(name, Util.null2String(property[i].getValue()));
        }
        baseBean.writeLog("MMI030物料主数据创建接口=======mainTableInfoMap:" + com.alibaba.fastjson.JSONObject.toJSONString(mainTableInfoMap));
        String workflowid = requestInfo.getWorkflowid();
        //获取表id
        writeLog("start------------------------ContractTriggersProcurementAction" + requestid);
        RecordSet rs = new RecordSet();
        rs.execute("select tablename from workflow_base a,workflow_bill b where a.formid=b.id and a.id=" + workflowid);
        rs.next();
        //获取主表名
        String maintable = Util.null2String(rs.getString("tablename"));

        //根据表id和主表名查找主表的相关信息
        rs.executeQuery("select * from " + maintable + " where requestid=?", new Object[]{requestid});
        rs.next();
        String lcbh = Util.null2String(rs.getString("lcbh"));//流程编号
        String sqlx = Util.null2String(rs.getString("sqlx"));//申请类型
        String wloah = Util.null2String(rs.getString("wloah"));//物料OA号
        writeLog("lcbhlcbhlcbhlcbhlcbh"+lcbh);
        writeLog("sqlxsqlxsqlxsqlxsqlx"+sqlx);
        writeLog("wloahwloahwloahwloah"+wloah);

        try {
            JSONObject result = new JSONObject();
            JSONArray DATA = new JSONArray();
            JSONObject data = new JSONObject();
            if (sqlx.equals("I")) {
                data.put("ZOAID", lcbh);
            } else {
                data.put("ZOAID", wloah);
            }

            data.put("ZOAHID", 1);
            data.put("ZDLBM",mainTableInfoMap.get("wldlbmzx"));
            // 增加字段
            data.put("ZDJZH",mainTableInfoMap.get("djzh"));

            data.put("MBRSH", mainTableInfoMap.get("xyly"));
            data.put("MTART", mainTableInfoMap.get("wllx"));
            data.put("MAKTX", mainTableInfoMap.get("wlms"));
            data.put("ZLMAKTX", mainTableInfoMap.get("wlcms"));
            data.put("BISMT", mainTableInfoMap.get("jwlh"));
            data.put("MEINS", mainTableInfoMap.get("jbdw"));
            if (mainTableInfoMap.get("sqlx").equals("I")){
                data.put("MATKL", mainTableInfoMap.get("wlzzx"));
            }else if(mainTableInfoMap.get("sqlx").equals("C")){
                if (!StringUtil.isNullOrEmpty(String.valueOf(mainTableInfoMap.get("xwlz")))){
                    data.put("MATKL", mainTableInfoMap.get("xwlz"));
                }else {
                    String  xzwl = String.valueOf(mainTableInfoMap.get("xzwl"));
                    RecordSet recordSet = new RecordSet();
                    recordSet.executeQuery("select wlz from uf_wlbd where wlbm=?",xzwl);
                    recordSet.next();
                    String wlz = Util.null2String(recordSet.getString("wlz"));
                    data.put("MATKL", wlz);

                }

            }else {
                if (!StringUtil.isNullOrEmpty(String.valueOf(mainTableInfoMap.get("xwlz")))){
                    data.put("MATKL", mainTableInfoMap.get("xwlz"));
                }else {
                    String  xzwl = String.valueOf(mainTableInfoMap.get("xzwl"));
                    RecordSet recordSet = new RecordSet();
                    recordSet.executeQuery("select wlz from uf_wlbd where wlbm=?",xzwl);
                    recordSet.next();
                    String wlz = Util.null2String(recordSet.getString("wlz"));
                    data.put("MATKL", wlz);

                }
            }


            data.put("ZWXDJ", mainTableInfoMap.get("wxdj"));
            data.put("ZXGLX", mainTableInfoMap.get("sqlx"));
            data.put("GROES", mainTableInfoMap.get("bzgg"));
            data.put("ZNAME", mainTableInfoMap.get("spmc"));
            data.put("ZNAME1", mainTableInfoMap.get("tym"));
            data.put("ZHL", mainTableInfoMap.get("hl"));
            data.put("ZFL", mainTableInfoMap.get("fl"));
            data.put("BRGEW", mainTableInfoMap.get("mz"));
            data.put("NTGEW", mainTableInfoMap.get("jz"));
            data.put("ZMD", mainTableInfoMap.get("md"));
            if(!mainTableInfoMap.get("sqlx").equals("I")) {
                data.put("MATNR",mainTableInfoMap.get("wlbm"));
            }
            DATA.add(data);

            result.put("DATA",DATA);
            baseBean.writeLog("wyq==MMI030物料主数据创建接口====object" + result.toString());

            String post = YNHttpUtils.MMI030(result.toString(), YNHttpUtils.token());
            baseBean.writeLog("wyq===MMI030物料主数据创建接口===post" + post);

            JSONObject jsonObject =  JSONObject.parseObject(post);
            JSONArray jsonArray = jsonObject.getJSONArray("RETURN");

            String status = jsonArray.getJSONObject(0).getString("STATUS");
            String message = jsonArray.getJSONObject(0).getString("MESSAGE");
            String MATNR = jsonArray.getJSONObject(0).getString("MATNR");

            RecordSet recordSet = new RecordSet();
            String updateMainTableName = "update " + tableName + " set  qqcsx = ? , fhjgx = ?  where  requestid = ? ";
            recordSet.executeUpdate(updateMainTableName, result.toString(), post, requestid);

            if (!"S".equals(status)) {
                requestInfo.getRequestManager()
                        .setMessageid(requestInfo.getRequestid()+"OA-MMI030物料主数据创建接口");
                requestInfo.getRequestManager().setMessagecontent("SAP接口异常，请联系SAP人员，错误信息提示:"+message);
                return Action.FAILURE_AND_CONTINUE;
            }

            //类型为I是更新主表wlbm
            if(mainTableInfoMap.get("sqlx").equals("I")) {

                String sql = "update "+ tableName + " set wlbm = ?   where  requestid = ? ";
                baseBean.writeLog("wyq===MMI030物料主数据创建接口===update" + sql);
                recordSet.executeUpdate(sql,MATNR,requestid);

            }





        } catch (Exception e) {
            baseBean.writeLog("wyq==MMI030=====exc"+e.toString());

            requestInfo.getRequestManager()
                    .setMessageid(requestInfo.getRequestid()+"OA-MMI030物料主数据创建接口");
            requestInfo.getRequestManager().setMessagecontent("接口异常"+e.toString());
            return Action.FAILURE_AND_CONTINUE;


        }

        return Action.SUCCESS;
    }
}
